import { DatabaseEnvStore } from "@/modules/dev-env/database/common/database-env-store.ts";
import type { LocalVersionItem } from "@/modules/dev-env/common/types.ts";
import path from "@/framework/util/path.ts";
import type { BaseEnvMirror } from "@/modules/dev-env/mirror/mirror.ts";
import {
  MariadbAliyunMirror,
  MariadbNeusoftMirror,
  MariadbNyistMirror,
  MariadbTsinghuaMirror,
  MariadbVhostMirror,
} from "@/modules/dev-env/database/mariadb/mariadb-mirror.ts";
import type { EnvTableAction } from "@/modules/dev-env/store/types.ts";
import { openInTerminal } from "@/util/system-utils2.ts";
import MariadbSettings from "@/modules/dev-env/database/mariadb/mariadb-settings.vue";

export class MariadbEnvStore extends DatabaseEnvStore<MariadbEnvStore> {
  constructor() {
    super("mariadb");

    this.icon = "icon-mariadb";
    this.pageConfig = {
      showCurrentAction: false,
    };

    this.tableActions = this._tableActions();
    this.settingsComponent = MariadbSettings;
  }

  private _tableActions(): EnvTableAction[] {
    return [
      {
        key: "config-install",
        icon: "icon-setting",
        name: "配置/安装",
        onClick: version => (this.proxy.selectedItem = version),
      },
      {
        key: "open-in-shell",
        icon: "icon-terminal",
        name: "在终端打开",
        onClick: version => openInTerminal(version.installPath, { subPath: ["bin"] }),
      },
      {
        key: "mysqldump",
        icon: "icon-dump",
        name: "mysqldump(开发中)",
        onClick: version => void 0,
        disabled: true,
      },
    ];
  }

  protected getMirrors0(): BaseEnvMirror[] {
    return [new MariadbTsinghuaMirror(), new MariadbAliyunMirror(), new MariadbNeusoftMirror(), new MariadbNyistMirror(), new MariadbVhostMirror()];
  }

  protected async getVersionInfo(versionDir: string): Promise<LocalVersionItem | undefined> {
    const folderName = path.basename(versionDir);
    if (!folderName.startsWith("mariadb-")) return undefined;

    const mysqld = path.join(versionDir, "bin/mysqld.exe");
    const mariadbd = path.join(versionDir, "bin/mariadbd.exe");
    if (!(await native.fs.exists(mysqld)) && !(await native.fs.exists(mariadbd))) return undefined;

    const [, version, _] = folderName.split("-");
    return {
      version: version,
      installPath: versionDir,
    };
  }
}
